package server;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class BackEnd {

	//users
	//  - list of users with their hostnames and status
	//connection stuff
	
	ArrayList<String[]> users;
	int[][] userFriends;

	//username, hostname, email
	protected BackEnd() {
		users = new ArrayList<String[]>(); //read in users

		FileInputStream fUsers = null;
		try {
			fUsers = new FileInputStream("users.txt");
		} catch (FileNotFoundException e) {
			System.out.println("ERROR - unable to locate users.txt.");
			e.printStackTrace();
		}
		Scanner userScanner = new Scanner(fUsers);

		while (userScanner.hasNext()) {
			String path = userScanner.next();

			Scanner lineScanner = new Scanner(path);
			lineScanner.useDelimiter(",");
			String[] store = new String[3];
			int count = 0;
			while (lineScanner.hasNext()) {
				String s = lineScanner.next();
				store[count] = s;
				count++;
			}

			users.add(store);
			users.toString();
		}

		FileInputStream fFriends = null;
		try {
			fFriends = new FileInputStream("friends.txt");
		} catch (FileNotFoundException e) {
			System.out.println("ERROR - unable to locate friends.txt.");
			e.printStackTrace();
		}
		Scanner friendScanner = new Scanner(fFriends);

		int topCount = 0;
		while (friendScanner.hasNext()) {
			String path = friendScanner.next();

			Scanner lineScanner = new Scanner(path);
			lineScanner.useDelimiter(",");
			int[] store = new int[10];
			int count = 0;
			while (lineScanner.hasNextInt()) {
				int s = lineScanner.nextInt();
				store[count] = s;
				count++;
			}

			for(int i = 0; i < store.length; i++) {
				System.out.print(store[i]+" - ");
			}
			System.out.println("\n");
//			userFriends[topCount] = store;
			System.out.println(store.toString());
			topCount++;
		}
		
	}

	public ArrayList<String[]> getUsers() {
		return users;
	}
	
	public String[] getUsernames() {
		String[] usernames = new String[users.size()];
		
		int count = 0;
		for (int i = 0; i <= users.size()-1; i++) {
			usernames[count] = users.get(i)[0];
			count++;
		}
		
		return usernames;
	}
	
	public String[] getUserComputers() {
		String[] computerNames = new String[users.size()];
		
		int count = 0;
		for (int i = 0; i <= users.size()-1; i++) {
			computerNames[count] = users.get(i)[1];
			count++;
		}
		
		return computerNames;
	}
	
	public String[] getUserEmails() {
		String[] emails = new String[users.size()];
		
		int count = 0;
		for (int i = 0; i <= users.size()-1; i++) {
			emails[count] = users.get(i)[2];
			count++;
		}
		
		return emails;
	}

	public int[][] getUserFriends() {
		return userFriends;
	}
	
	public void setUsers(ArrayList<String[]> users) {
		this.users = users;
	}

	public void setUserFriends(int[][] userFriends) {
		this.userFriends = userFriends;
	}

	protected void newUser(String n, String c, String e) {
		String[] user = {n, c, e};
		users.add(user);
	}
}
